﻿using AutoMapper;
using PaPaPa.Web.MappingRelationship.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PaPaPa.Web.MappingRelationship.Base
{
    public class BaseMapper<Model, Entity> : IRelationalMapping<Model, Entity>
        where Model : class
        where Entity : class
    {
        public BaseMapper()
        {
            Mapper.CreateMap<Model, Entity>();
            Mapper.CreateMap<Entity, Model>();
        }

        public Entity GetEntity(Model model)
        {
            var entity = Mapper.Map<Model, Entity>(model);

            return entity;
        }

        public Model GetModel(Entity entity)
        {
            var dto = Mapper.Map<Entity, Model>(entity);

            return dto;
        }

        public List<Entity> GetEntityList(List<Model> lstModel)
        {
            var lstEntity = new List<Entity>();

            lstModel.ForEach(dto =>
            {
                lstEntity.Add(Mapper.Map<Model, Entity>(dto));
            });

            return lstEntity;
        }

        public List<Model> GetModelList(List<Entity> lstEntity)
        {
            var lstDTO = new List<Model>();

            lstEntity.ForEach(entity =>
            {
                lstDTO.Add(Mapper.Map<Entity, Model>(entity));
            });

            return lstDTO;
        }
    }
}
